//加载公共样式
require('../../assets/css/index.less')
//axios
const request = require('../../utils/request')
const dom = require('../../utils/dom')
//字体图标
require('../../assets/fonts/iconfont.css')

//引入swiper的css
require('swiper/dist/css/swiper.min.css')
//引入swiper
const mySwiper = require('swiper')
// weui
require('../../lib/weui/weui.css')
let weui = require('../../lib/weui/weui.js')

//加载自己的样式
require('./home.less')


window.onload = () => {
    //底部
    dom.Bmenu('home')

    //使用swiper
    let mySwiper = new Swiper('.swiper-container', {
        autoplay: 3000, //可选选项，自动滑动
        initialSlide: 0,
        // effect: 'fade',
        direction: 'horizontal',
        pagination: '.swiper-pagination', //分页器
        loop: true, //无限循环
    })


    const rankNum = document.querySelector('#rankNum')
    const clockBtn = document.querySelector('#clockBtn') //打卡
    const clockCountVal = document.querySelector('#clockCountVal') //累计打卡
    const badgesVal = document.querySelector('#badgesVal')

    //打卡信息
    function getClock() {
        request.get('/api/user/clockInInfo').then(res => {
            console.log('打卡信息', res);
            let { data, errno } = res.data
            if (errno == 0) {
                clockCountVal.textContent = data.clockCount
                clockBtn.textContent = data.isClockIn ? '已经打过啦' : '今日打卡'
            }
        })
    }
    getClock()



    //个人信息
    function getUserInfo() {
        request.get('/api/user/info').then(res => {
            console.log(res);
            let { data, errno } = res.data
            if (errno == 0) {
                render(data)
            }
        })
    }
    function render(data) {
        rankNum.textContent = data.ranking
        badgesVal.textContent = data.badges
    }

    getUserInfo()

    //打卡
    function getClockIn() {
        clockBtn.addEventListener('click', function () {
            request.post('/api/user/clockIn').then(res => {
                console.log('打卡', res);
                let { errno } = res.data
                if (errno == -1) {
                    weui.alert('已经打过卡了，明天再来吧')
                } else {
                    weui.alert('恭喜您，成功打卡')
                    getClock()
                    getUserInfo()
                }
            })
        })
    }
    getClockIn()


}



